<template>
  <div class="navbar">

    <div class="nav-container">
      <a-col flex="300px">
      <div class="logo">
        <router-link to="/">
          <span class="logo-text">聚美优品</span>
        </router-link>
      </div>
      </a-col>

      <a-col flex="auto">
      <div class="nav-menu">
        <router-link to="/" class="nav-item" :class="{ active: $route.path === '/' }">
          <HomeOutlined />
          <span>首页</span>
        </router-link>
        <router-link to="/cart" class="nav-item" :class="{ active: $route.path.startsWith('/cart') }">
          <ShoppingCartOutlined />
          <span>购物车</span>
        </router-link>
        <router-link to="/orders" class="nav-item" :class="{ active: $route.path.startsWith('/orders') }">
          <FileTextOutlined />
          <span>订单</span>
        </router-link>
        <router-link to="/profile" class="nav-item" :class="{ active: $route.path.startsWith('/profile') }">
          <UserOutlined />
          <span>个人中心</span>
        </router-link>
      </div>
      </a-col>
      <a-button v-if="loginUserStore.loginUser.userRole===ACCESS_ENUM.ADMIN" type="primary" ghost @click="goToAdmin" class="admin-btn" style="margin-right: 40px">
        <SettingOutlined />
        <span class="btn-text">管理后台</span>
      </a-button>
<!--      <div class="nav-actions">
        <a-button type="primary" ghost @click="goToAdmin" class="admin-btn">
          <SettingOutlined />
          <span class="btn-text">管理后台</span>
        </a-button>
        <a-button type="primary" @click="goToLogin" class="login-btn">
          <LoginOutlined />
          <span class="btn-text">登录</span>
        </a-button>
      </div>-->
      <a-col flex="120px">

        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <ASpace>
                <a-avatar
                  :src="loginUserStore.loginUser.userAvatar"
                  class="avatar"
                >
                  {{ loginUserStore.loginUser.userName ?? '无名' }}
                </a-avatar>
              </ASpace>
              <template #overlay>
                <a-menu class="dropdown-menu">
                  <a-menu-item @click="doLogout">
                    <LoginOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
            <span class="user-name">{{ loginUserStore.loginUser.userName ?? '无名' }}</span>
          </div>
          <div v-else>
            <a-button type="primary" @click="goToLogin" class="login-btn">
              <LoginOutlined />
              <span class="btn-text">登录</span>
            </a-button>
          </div>
        </div>
      </a-col>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import {
  HomeOutlined,
  ShoppingCartOutlined,
  FileTextOutlined,
  UserOutlined,
  SettingOutlined,
  LoginOutlined
} from '@ant-design/icons-vue'
import {useLoginUserStore} from "@/stores/user.ts";
import {message} from "ant-design-vue";
import {userLogout} from "@/api/userController.ts";
import ACCESS_ENUM from "@/utils/access/ACCESS_ENUM.ts";
const loginUserStore = useLoginUserStore()
const router = useRouter()

const goToAdmin = () => {
  router.push('/admin')
}

const goToLogin = () => {
  router.push('/login')
}

const doLogout = async () => {
  const res = await userLogout()
  if(res.data.code === 0) {
    loginUserStore.setLoginUser({ userName: '未登录' })
    message.success('退出登录成功')
    await router.push('/login')
  } else {
    message.error('退出登录失败'+res.data.msg)
  }
}

</script>

<style scoped>
.navbar {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
  width: 100%;
}

.nav-container {
  width: 100%;
  max-width: 100%;
  margin: 0;
  display: flex;
  align-items: center;
  padding: 0 24px;
  height: 64px;
}

.logo {
  margin-right: 48px;
  flex-shrink: 0;
}

.logo a {
  text-decoration: none;
}

.logo-text {
  font-size: 28px;
  font-weight: 700;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.nav-menu {
  flex: 1;
  display: flex;
  gap: 8px;
  justify-content: center;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  color: rgba(255, 255, 255, 0.9);
  text-decoration: none;
  border-radius: 8px;
  transition: all 0.3s ease;
  font-weight: 500;
  white-space: nowrap;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  transform: translateY(-1px);
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.nav-item span {
  font-size: 14px;
}

.nav-actions {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-shrink: 0;
}

.admin-btn {
  border-color: rgba(255, 255, 255, 0.3);
  color: white;
  display: flex;
  align-items: center;
  gap: 4px;
}

.admin-btn:hover {
  border-color: white;
  background: rgba(255, 255, 255, 0.1);
  color: white;
}

.login-btn {
  background: white;
  color: #667eea;
  border: none;
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 600;
}

.login-btn:hover {
  background: #f8f9fa;
  color: #667eea;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .nav-container {
    padding: 0 20px;
  }

  .logo {
    margin-right: 32px;
  }

  .logo-text {
    font-size: 24px;
  }
}

@media (max-width: 992px) {
  .nav-container {
    padding: 0 16px;
  }

  .logo {
    margin-right: 24px;
  }

  .nav-menu {
    gap: 4px;
  }

  .nav-item {
    padding: 6px 12px;
  }

  .nav-item span {
    font-size: 13px;
  }
}

@media (max-width: 768px) {
  .nav-container {
    padding: 0 12px;
  }

  .logo {
    margin-right: 16px;
  }

  .logo-text {
    font-size: 20px;
  }

  .nav-menu {
    display: none;
  }

  .nav-actions {
    gap: 8px;
  }

  .btn-text {
    display: none;
  }
}

@media (max-width: 480px) {
  .nav-container {
    padding: 0 8px;
  }

  .logo-text {
    font-size: 18px;
  }

  .nav-actions {
    gap: 4px;
  }
}
</style>
